package cos.android.voleysplash.element;

import java.util.Vector;

import android.graphics.Bitmap;
import android.graphics.Rect;
import cos.android.voleysplash.level.Point;

public class Element implements Cloneable {

	private Bitmap sprite;
	private Bitmap colisionSprite;

	private String name;
	private Point origin;
	private String kind;

	private int height;
	private int width;

	private Rect rect;

	private int floor = 0;

	private DinamicElement dinamicElement;
	private Circle circle;
	private Polygon polygon;
	private Breakable breakable;

	public Element(Bitmap sprite, Bitmap colisionSprite, int xPos, int yPos,
			String kind) {

		super();
		name = "";
		this.sprite = sprite;
		origin = new Point(xPos, yPos);
		this.height = sprite.getHeight();
		this.width = sprite.getWidth();
		this.rect = new Rect(0, 0, getWidth(), getHeight());
		this.kind = kind;
		this.colisionSprite = colisionSprite;

		dinamicElement = null;
		circle = null;
		polygon = null;
		breakable = null;
	}

	public int getFloor() {
		return floor;
	}

	public void setFloor(int floor) {
		this.floor = floor;
	}

	public Bitmap getSprite() {
		return sprite;
	}

	public void setSprite(Bitmap sprite) {
		this.sprite = sprite;
	}

	public Bitmap getColisionSprite() {
		return colisionSprite;
	}

	public void setColisionSprite(Bitmap colisionSprite) {
		this.colisionSprite = colisionSprite;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public Point getOrigin() {
		return origin;
	}

	public void setOrigin(Point origin) {
		this.origin = origin;
	}

	public String getKind() {
		return kind;
	}

	public void setKind(String kind) {
		this.kind = kind;
	}

	public int getHeight() {
		return height;
	}

	public void setHeight(int height) {
		this.height = height;
	}

	public int getWidth() {
		return width;
	}

	public void setWidth(int width) {
		this.width = width;
	}

	public Rect getRect() {
		return rect;
	}

	public void setRect(Rect rect) {
		this.rect = rect;
	}

	public boolean isDinamicElement() {
		return dinamicElement != null;
	}

	public void setDinamicElement(DinamicElement dinamicElement) {
		this.dinamicElement = dinamicElement;
	}

	public Element getLastHitted() {
		if (dinamicElement != null) {
			return dinamicElement.getLastHitted();
		}
		return null;
	}

	public void setLastHitted(Element element) {
		if (dinamicElement != null) {
			dinamicElement.setLastHitted(element);
		}
	}

	public float getoX() {
		if (dinamicElement != null) {
			return dinamicElement.getoX();
		}
		return 0;
	}

	public float getoY() {
		if (dinamicElement != null) {
			return dinamicElement.getoY();
		}
		return 0;
	}

	public void applySpeed(float ox, float oy) {
		if (dinamicElement != null) {
			if ((getKind().equals("Rampa")) || (getKind().equals("Rampa2"))
					|| (getKind().equals("HorizontalWall"))
					|| (getKind().equals("VerticalWall"))) {
				System.out.println("Se movio el muro!!" + getKind());
			}
			dinamicElement.applySpeed(ox, oy);
		}
	}

	public float getSpeed() {
		if (dinamicElement != null) {
			return dinamicElement.getSpeed();
		} else {
			return 0f;
		}
	}

	public void update(float elapsed) {
		if (dinamicElement != null) {
			dinamicElement.update(this, elapsed);
		}
	}

	public Circle getCircle() {
		return circle;
	}

	public void setCircle(Circle circle) {
		this.circle = circle;
	}

	public boolean isPolygon() {
		return polygon != null;
	}

	public void setPolygon(Polygon polygon) {
		this.polygon = polygon;
	}

	public boolean isBreakable() {
		return breakable != null;
	}

	public void setBreakable(Breakable breakable) {
		this.breakable = breakable;
	}

	public Vector<Point> getAristas() {
		if (polygon != null) {
			return polygon.getRealAristas(origin);
		} else {
			return circle.getAristas(origin, height);
		}
	}

	public boolean isCircle() {

		return circle != null;
	}

	public float getCenterX() {
		if (isCircle()) {
			return circle.getCenterX(origin, height);
		} else {
			return (getxPos() + getWidth() / 2);
		}
	}

	public float getCenterY() {
		if (isCircle()) {
			return circle.getCenterY(origin, height);
		} else {
			return (getyPos() + getHeight() / 2);
		}
	}

	public float getxPos() {
		return origin.getxPos();
	}

	public float getyPos() {
		return origin.getyPos();
	}

	public float getRadius() {
		return circle.getRadius(getHeight());
	}

	public void setxPos(float xPos) {
		origin.setxPos(xPos);
	}

	public void setyPos(float yPos) {
		origin.setyPos(yPos);
	}

	public void refreshAristas() {

	}

	public Point collition(Element element) {
		if (isCircle()) {
			return circle.colition(origin, element, height);
		} else {
			return polygon.collition(origin, element);
		}
	}

	public boolean collition(Point point) {
		if (isCircle()) {
			if (circle.colition(origin, point, height)) {
				return true;
			} else {
				return false;
			}
		} else {
			if (polygon.collition(origin, point)) {
				return true;
			} else {
				return false;
			}
		}
	}

	public void dispose() {
		setOrigin(new Point(-500, -500));
		sprite = null;
	}

	public void resetY() {
		// TODO Auto-generated method stub

	}

	public void reset() {
		// TODO Auto-generated method stub

	}

	public void reactToColition(Point contactPoint) {

		if (isCircle()) {
			float dx = circle.getCenterX(origin, height)
					- contactPoint.getxPos();
			float dy = circle.getCenterY(origin, height)
					- contactPoint.getyPos();

			float dxb = Math.abs(circle.getCenterX(origin, height)
					- contactPoint.getxPos());
			float dyb = Math.abs(circle.getCenterY(origin, height)
					- contactPoint.getyPos());

			float propY = (dyb / (dxb + dyb));
			float propX = 1 - propY;

			float previousSpeed = getSpeed();
			reset();
			System.out.println("contact: " + contactPoint.toString());
			System.out.println("center: " + circle.getCenterX(origin, height)
					+ " " + circle.getCenterY(origin, height));
			System.out.println("x: " + propX + " " + Math.signum(dx));
			System.out.println("y: " + propY + " " + Math.signum(dy));
			System.out.println("");
			applySpeed(previousSpeed * propX * Math.signum(dx), Math.signum(dy)
					* previousSpeed * propY);

		}
	}

}
